package oop.core.med;

import java.util.HashMap;
import java.util.Map;

import oop.core.IDUpdateable;
import oop.core.Scheduler;
import oop.core.entity.Entity;

/**
 * In charge of scheduling delayed flag toggles
 * @author DangMH
 *
 */
public class DelayMediator implements IDUpdateable
{
	private Scheduler scheduler;
	private Map <Integer, Entity> IDmap;
	private int ID;

	/**
	 * Empty constructor.
	 */
	public DelayMediator()
	{
		scheduler = new Scheduler();
		IDmap = new HashMap<Integer, Entity>();
		ID = 0;
	}
	
	/**
	 * Parameterized constructor.
	 */
	public DelayMediator( Scheduler scheduler )
	{
		this.scheduler = scheduler;
		IDmap = new HashMap<Integer, Entity>();
		ID = 0;
	}
	
	/**
	 * Scheduler interface
	 */
	
	public void update(long ID) {
		clearDelayFlag( ID );
	}
	
	/**
	 * Interface to "wake up" entities from delays
	 * @param ID
	 */
	public void clearDelayFlag( long ID )
	{
		IDmap.get(ID).clearDelayFlag();
	}
	
	/**
	 * Operation to register a delay on an Entity
	 * @param e
	 * @param delay
	 */
	public void register( Entity e, long delay )
	{
		e.setDelayFlag();
		IDmap.put( ID, e);
		scheduler.register( this, delay, ID++ );
	}
}
